package com.zg.monitor.os.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zg.monitor.common.util.LayoutUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author Zxb
 */
@Data
@Builder
@Accessors(chain = true)
public class Thread {
    /**
     * 活动线程数
     */
    private int threadCount;

    /**
     * 峰值活动线程数
     */
    private int peakThreadCount;

    /**
     * 守护线程数
     */
    private int daemonThreadCount;

    /**
     * 非守护线程数
     */
    private int nonDaemonThreadCount;

    /**
     * 总线程数
     */
    private long totalStartedThreadCount;

    @Override
    public String toString() {
        return LayoutUtil.Table.of(
                LayoutUtil.Row.of("Active Thread Count", threadCount),
                LayoutUtil.Row.of("Active Peak Thread Count", peakThreadCount),
                LayoutUtil.Row.of("Daemon Thread Count", daemonThreadCount),
                LayoutUtil.Row.of("NonDaemon Thread Count", nonDaemonThreadCount),
                LayoutUtil.Row.of("Total Thread Count", totalStartedThreadCount)
        ).toString();
    }

    public JSONArray toJSON() {
        JSONArray ja = new JSONArray();
        JSONObject jo = new JSONObject();
        jo.put("activeThreadCount", threadCount);
        jo.put("activePeakThreadCount", peakThreadCount);
        jo.put("daemonThreadCount", daemonThreadCount);
        jo.put("nonDaemonThreadCount", nonDaemonThreadCount);
        jo.put("totalThreadCount", totalStartedThreadCount);
        ja.add(jo);
        return ja;
    }
}
